unit unPontuacao;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, Grids, DBGrids, ThemeDBGrid, DB, StdCtrls, Buttons, ComCtrls;

type
 TfmPontuacao = class(TForm)
    dsPontuacao: TDataSource;
  btFechar: TBitBtn;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    ThemeDBGrid1: TThemeDBGrid;
    btDetalhes: TBitBtn;
    PageControl2: TPageControl;
    TabSheet2: TTabSheet;
    ThemeDBGrid2: TThemeDBGrid;
    dsDetalhesPontuacao: TDataSource;
    Label1: TLabel;
    btGrafico: TBitBtn;
  procedure btFecharClick(Sender: TObject);
  procedure FormCreate(Sender: TObject);
  procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure btDetalhesClick(Sender: TObject);
    procedure ThemeDBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure ThemeDBGrid1CellClick(Column: TColumn);
    procedure ThemeDBGrid2DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure FormShow(Sender: TObject);
    procedure dsPontuacaoDataChange(Sender: TObject; Field: TField);
    procedure btGraficoClick(Sender: TObject);
 private
    { Private declarations }
 public
    { Public declarations }
 end;
 
var
 fmPontuacao: TfmPontuacao;
 
implementation

uses unDM, unResultados, unDetalhePontuacao, unGrafico;

{$R *.dfm}

procedure TfmPontuacao.btDetalhesClick(Sender: TObject);
begin
fmDetalhePontuacao := TfmDetalhePontuacao.Create(Self);
dm.tabDetalhePontuacao.Close;
dm.tabDetalhePontuacao.SQL.Clear;
dm.tabDetalhePontuacao.SQL.Text := 'SELECT * from DetalhePontuacao WHERE idpiloto=:idpiloto';
dm.tabDetalhePontuacao.Params[0].AsInteger := dm.sqlPontuacaoidpiloto.Value;
dm.tabDetalhePontuacao.Open;
fmDetalhePontuacao.TabSheet1.Caption := dm.sqlPontuacaoNome.Value;
fmDetalhePontuacao.ShowModal;
fmDetalhePontuacao.Free;
end;

procedure TfmPontuacao.btFecharClick(Sender: TObject);
begin
 Close
end;

procedure TfmPontuacao.btGraficoClick(Sender: TObject);
begin
 fmGrafico := TfmGrafico.Create(Self);
 fmGrafico.ShowModal;
 fmGrafico.Free;
end;

procedure TfmPontuacao.dsPontuacaoDataChange(Sender: TObject; Field: TField);
begin
ThemeDBGrid1CellClick(ThemeDBGrid1.Columns[0]);
end;

procedure TfmPontuacao.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 dm.sqlPontuacao.Close;
 dm.tabDetalhePontuacao.Close;
end;

procedure TfmPontuacao.FormCreate(Sender: TObject);
begin
 dm.sqlPontuacao.Open;
 dm.tabDetalhePontuacao.Open;
end;

procedure TfmPontuacao.FormKeyPress(Sender: TObject; var Key: Char);
begin
 if key = #27 then
  Close;
end;

procedure TfmPontuacao.FormShow(Sender: TObject);
begin
ThemeDBGrid1.SetFocus;
ThemeDBGrid1CellClick(ThemeDBGrid1.Columns[0]);
end;

procedure TfmPontuacao.ThemeDBGrid1CellClick(Column: TColumn);
begin
dm.tabDetalhePontuacao.Close;
dm.tabDetalhePontuacao.SQL.Clear;
dm.tabDetalhePontuacao.SQL.Text := 'SELECT * from DetalhePontuacao WHERE idpiloto=:idpiloto';
dm.tabDetalhePontuacao.Params[0].AsInteger := dm.sqlPontuacaoidpiloto.Value;
dm.tabDetalhePontuacao.Open;
TabSheet2.Caption := dm.sqlPontuacaoNome.Value;
end;

procedure TfmPontuacao.ThemeDBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
 if (dm.sqlPontuacao.RecNo mod 2) = 0 then
  if ThemeDBGrid1.Canvas.Brush.Color = ThemeDBGrid1.Color then
   ThemeDBGrid1.Canvas.Brush.Color := clMoneyGreen;
 
 ThemeDBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

procedure TfmPontuacao.ThemeDBGrid2DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
 if (dm.tabDetalhePontuacaoPosicao.Value = 1) and (Column.Field = dm.tabDetalhePontuacaoPosicao) then
  ThemeDBGrid2.Canvas.Font.Style := ThemeDBGrid2.Canvas.Font.Style + [fsBold];
 
 if (dm.tabDetalhePontuacao.RecNo mod 2) = 0 then
  if ThemeDBGrid2.Canvas.Brush.Color = ThemeDBGrid2.Color then
   ThemeDBGrid2.Canvas.Brush.Color := clMoneyGreen;
 
 ThemeDBGrid2.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

end.

